<?php
namespace app\api\controller\kuyou\applet;
use think\Db;
class Replenishment extends Common
{
    public function index()
    {
        if ($this->request->isPost()) {
            $where = [];
            $goods_code = $this->request->post('goods_code');
            if(!empty($goods_code)){
                $where['goods_code'] = ["like", '%'.$goods_code.'%'];
            }

            $list_type = $this->request->post('list_type');
            $list_type = $list_type==1?$list_type:2;

            $goods_type = $this->request->post('goods_type');
            if(!empty($goods_type)){
                $goods_type = explode(',',$goods_type);
            }else{
                $goods_type = [];
            }

            $sales_list = Db::name('kuyou_goods_sales_order_info')->where($where)->group('goods_code,goods_attr')->field('goods_code,goods_attr')->select();
            $lists = [];
            foreach ($sales_list as $sales_list_k=>$sales_list_v){
                $row = [];
                $goods = Db::name('kuyou_goods')->where(['goods_code'=>$sales_list_v['goods_code']])->find();
                $row['goods_id'] = $goods['id'];
                $row['goods_code'] = $goods['goods_code'];
                $row['goods_name'] = $goods['goods_code'].'_'.$goods['goods_name'];
                $row['goods_image'] = $goods['goods_image'];
                $row['buy_price'] = $goods['purchase'];

                if($goods['min_stock']<=0){
                    $goods['min_stock'] = Db::name('kuyou_goods_stock_warning')->where(['goods_code'=>$goods['goods_code']])->sum('min_stock');
                }
                $row['min_stock'] = $goods['min_stock'];

                $where = [];
                $where['goods_code'] = $goods['goods_code'];
                if($sales_list_v['goods_attr']>0){
                    $where['attribute_id'] = $sales_list_v['goods_attr'];
                }
                $stock = Db::name('kuyou_goods_stock')->where($where)->field('stock,id,cost_money')->sum('stock');
                $row['stock'] = $stock;

                $row['attr_id'] = '';
                $row['attr_name'] = '';
                if($goods['auxiliary']==1){
                    //商品设置属性
                    $row['attr_id'] = $sales_list_v['goods_attr'];
                    $attribute_id = Db::name('kuyou_goods_attr')->where(['id'=>$sales_list_v['goods_attr']])->value('attribute_id');
                    $attribute_ids = Db::name('kuyou_aux_combination')->where(['id'=>$attribute_id])->value('attr_ids');
                    if($attribute_ids){
                        $attribute_ids = explode('&',$attribute_ids);
                        $attribute = Db::name('kuyou_auxiliary')->where(['id'=>['in',$attribute_ids]])->column('name');
                        $attribute_name = implode('/',$attribute);
                        $row['attr_name'] = $attribute_name;
                    }
                }

                //商品单位
                $unit = Db::name('kuyou_unit')->where(['id'=>$goods['unit_id']])->field('id,name')->find();
                $row['goods_unit'] = $goods['unit_id'];
                $row['unit_name'] = isset($unit['name'])?$unit['name']:'';

                $list = Db::name('kuyou_goods_sales_order_info')->where(['goods_code'=>$sales_list_v['goods_code'],'goods_attr'=>$sales_list_v['goods_attr']])->select();
                $row['number'] = 0;
                foreach ($list as $list_k=>$list_v){
                    $goods = Db::name('kuyou_goods')->where(['goods_code'=>$list_v['goods_code']])->find();
                    $number = $list_v['number'];
                    $row['number'] = $number;
                    if($goods['more_unit']==1 && $goods['unit_id']!=$list_v['goods_unit']){
                        $advance_rate = Db::name('kuyou_unit_conversion')->where(['main_unit'=>$goods['unit_id'],'vice_unit'=>$list_v['goods_unit']])->value('advance_rate');
                        if($advance_rate){
                            $row['number'] = $number*$advance_rate;
                        }
                    }
                }

                $list = Db::name('kuyou_goods_purchase_order_info')->where(['goods_code'=>$sales_list_v['goods_code'],'goods_attr'=>$sales_list_v['goods_attr']])->select();
                $row['buy_number'] = 0;
                foreach ($list as $list_k=>$list_v){
                    $goods = Db::name('kuyou_goods')->where(['goods_code'=>$list_v['goods_code']])->find();
                    $number = $list_v['number'];
                    $row['buy_number'] = $number;
                    if($goods['more_unit']==1 && $goods['unit_id']!=$list_v['goods_unit']){
                        $advance_rate = Db::name('kuyou_unit_conversion')->where(['main_unit'=>$goods['unit_id'],'vice_unit'=>$list_v['goods_unit']])->value('advance_rate');
                        if($advance_rate){
                            $row['buy_number'] = $number*$advance_rate;
                        }
                    }
                }
                $row['now_buy_number'] = $row['number']-$row['stock']-$row['buy_number']+$row['min_stock'];
                if($row['now_buy_number']<0){
                    $row['now_buy_number'] = 0;
                }

                if(!empty($goods_type)){
                    if(in_array($goods['goods_type_id'],$goods_type)){
                        if($list_type==1){
                            if($row['now_buy_number']>0){
                                $lists[] = $row;
                            }
                        }else{
                            $lists[] = $row;
                        }
                    }
                }else{
                    if($list_type==1){
                        if($row['now_buy_number']>0){
                            $lists[] = $row;
                        }
                    }else{
                        $lists[] = $row;
                    }
                }
            }
            $result = array("count" => sizeof($lists), "list" => $lists);

            if(isset($result['list']) && isset($result['count'])){
                $this->success('查询成功',$result,200);
            }else{
                $this->error('查询失败','',406);
            }
        }else{
            $this->error('请求方式错误','',250);
        }
    }
}
